package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.AddressBookMapper;
import com.example.demo.dto.vo.PageVo;
import com.example.demo.entity.AddressBook;
import com.example.demo.service.AddressBookService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;

/**
 * 通讯录服务
 *
 * @author wzw
 * @date 2022/4/27 14:41
 */
@Service
@RequiredArgsConstructor
public class AddressBookServiceImpl implements AddressBookService {
    // todo 2022-05-08 23:51:01 属性需添加相应注释文档
    /**
     * 用户id
     */
    private final String ID = "id";
    /**
     * 手机号
     */
    private final String PHONE = "phone";
    // todo 2022-05-08 23:53:52 常用数据用常量的思路非常好。不过用途都是判断对应数据是否存在，将其封装成方法供其它函数调用是否更好？
    // todo 2022-05-09 00:01:28 查一下private、protected、public与不加任何修饰符的区别，不要图省事就什么都不加

    private final AddressBookMapper addressBookMapper;


    @Override
    public AddressBook getAddressBookById(Long id) {
        // 根据id返回结果
        return addressBookMapper.selectById(id);
    }


    @Override
    public String removeAddressBook(Long id) {
        // 根据id查找数据中id存不存在
        // todo 2022-05-08 23:58:53 与96～98行代码重复
        if (addressBookMapper.selectCount(new QueryWrapper<AddressBook>().eq(ID, id)) == 0) {
            return "删除的id不存在";
        }

        addressBookMapper.deleteById(id);
        return "删除成功";
    }


    @Override
    public PageInfo<AddressBook> listAddressBooksByPage(PageVo pagevo) {
        // todo 2022-05-08 23:56:53 校验的思路很好，不过每个模块做分页查询都加下面的代码就不太好了。不着急解决，留做你的思考题

        // 进行分页查询
        Page<AddressBook> pageData = PageHelper.startPage(pagevo.getPage(), pagevo.getPageSize());
        QueryWrapper<AddressBook> addressBookQueryWrapper = new QueryWrapper<>();
        pageData.doSelectPage(() -> addressBookMapper.selectList(addressBookQueryWrapper));

        return new PageInfo<>(pageData);
    }

    @Override
    public String insertAddressBook(String name, String phone, LocalDate birthday, String remark) {
        // 判断新增条件是否成立并且返回信息
        if (addressBookMapper.selectCount(new QueryWrapper<AddressBook>().eq(PHONE, phone)) != 0) {
            return "手机号码已存在";
        }

        // 创建新通讯录并且返回信息
        AddressBook addressBook = new AddressBook();
        addressBook.setName(name);
        addressBook.setPhone(phone);
        addressBook.setBirthday(String.valueOf(birthday));
        addressBook.setRemark(remark);
        addressBook.setCreateTime(new Date());
        addressBookMapper.insert(addressBook);
        return "新增成功";
    }

    @Override
    public String updateAddressBook(Long id, String name, String phone, LocalDate birthday, String remark) {
        // 判断修改条件是否成立并且返回信息
        if (addressBookMapper.selectCount(new QueryWrapper<AddressBook>().eq(ID, id)) == 0) {
            return "更新的id不存在";
        }
        if (addressBookMapper.selectCount(new QueryWrapper<AddressBook>().eq(PHONE, phone)) == 1 && !phone.equals(addressBookMapper.selectById(id).getPhone())) {
            return "手机号码已存在";
        }
        // 修改通讯录并返回信息
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        addressBook.setName(name);
        addressBook.setPhone(phone);
        addressBook.setBirthday(String.valueOf(birthday));
        addressBook.setRemark(remark);
        addressBook.setModifiedTime(new Date());
        addressBookMapper.updateById(addressBook);
        return "修改成功";
    }

}
